package com.example.mybatisplues.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.example.mybatisplues.Enum.UserStatusEnum;
import com.example.mybatisplues.mapper.UserMapper;
import com.example.mybatisplues.po.Address;
import com.example.mybatisplues.po.PageResult;
import com.example.mybatisplues.po.User;
import com.example.mybatisplues.po.UserPage;
import com.example.mybatisplues.service.UserService;
import com.example.mybatisplues.util.MyPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    @Transactional
    public void deductMoneyById(Integer id, Integer money) {
        //判断用户状态
        User user = userMapper.selectById(id);
        if (user != null || user.getStatus() == UserStatusEnum.Active) {
            if (user.getBalance() >= money) {
                //扣钱
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<User>().eq(User::getId, id);
                userMapper.deductMoneyById(userLambdaQueryWrapper, money);
            } else System.out.println("钱不够");
        } else {
            System.out.println("账号异常");
        }
    }

    @Override
    public List<User> queryUserlist(String name, Integer status, Integer minBalance, Integer maxBalance) {
        System.out.println(name + "" + status + "" + minBalance + "" + maxBalance);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<User>()
                .like(name != null, User::getUsername, name)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance);
        List<User> users = userMapper.selectList(userLambdaQueryWrapper);
        return users;
    }

    @Override
    public User getAddressAndUserbyid(Integer id) {
        User user = userMapper.selectById(id);
        if (user != null && user.getStatus() == UserStatusEnum.Active) {
            List<Address> list = Db.lambdaQuery(Address.class).eq(Address::getUserId, id).list();
            user.setAddresses(list);
        } else {
            throw new RuntimeException("用户状态异常");
        }
        return user;
    }

    @Override
    public List<User> getAddressAndUserbyids(List<Integer> ids) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<User>().in(User::getId, ids);
        List<User> users = userMapper.selectList(userLambdaQueryWrapper);
        if (users != null) {
            for (User user : users) {
                user.setAddresses(Db.lambdaQuery(Address.class).eq(Address::getUserId, user.getId()).list());
            }
        } else {
            throw new RuntimeException("用户状态异常");
        }
        return users;
    }

    @Override
    public PageResult queryPage(UserPage userPage) {
        Page<User> page = MyPageUtils.getUserPage(userPage);
        Page<User> page2 = lambdaQuery()
                .like(User::getUsername, userPage.getName())
                .eq(User::getStatus, userPage.getStatus()).page(page);
        PageResult pageResult = MyPageUtils.getPageResult(page2);
        return pageResult;
    }


}
